//
//  PowerValueView.swift
//  SCA
//
//  Created by tjun on 2025/9/23.
//

import UIKit


class PowerCircleStackView: UIStackView {
    var segment: PowerSegment?
}


class PowerValueView: UIView {

    var onPowerCircleInfoTapped: ((PowerModel) -> Void)?

    var segments: [PowerSegment] = [] {
        didSet {setupLegend() }
    }
    
    var powerInfo: PowerInfo? {
        didSet {
            guard let powerInfo = powerInfo,
                  let mintingModel = powerInfo.cat[.minting],
                  let matrixModel = powerInfo.cat[.matrix],
                  let differentialModel = powerInfo.cat[.differential],
                  let peerModel = powerInfo.cat[.peer] else { return }
                        
            segments = [
                PowerSegment(value: CGFloat((mintingModel.percent as NSString).floatValue), color: UIColor.hexStringColor(hexString: mintingModel.color), title: mintingModel.title, model: mintingModel),
                
                PowerSegment(value: CGFloat((matrixModel.percent as NSString).floatValue), color: UIColor.hexStringColor(hexString: matrixModel.color), title: matrixModel.title, model: matrixModel),
                
                PowerSegment(value: CGFloat((differentialModel.percent as NSString).floatValue), color: UIColor.hexStringColor(hexString: differentialModel.color), title: differentialModel.title, model: differentialModel),
                PowerSegment(value: CGFloat((peerModel.percent as NSString).floatValue), color: UIColor.hexStringColor(hexString: peerModel.color), title: peerModel.title, model: peerModel),
            ]
        }
    }
    
    private let legendStack = UIStackView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        setupLegendStack()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupLegendStack() {
        
        legendStack.axis = .vertical
        legendStack.spacing = 8
        legendStack.alignment = .leading
        addSubview(legendStack)
        legendStack.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.centerY.equalToSuperview()
        }
    }
    
    private func setupLegend() {
        legendStack.arrangedSubviews.forEach { $0.removeFromSuperview() }
        for segment in segments {
            let item = legendItem(color: segment.color, title: segment.title)
            item.segment = segment
            legendStack.addArrangedSubview(item)
        }
    }
    
    private func legendItem(color: UIColor, title: String) -> PowerCircleStackView {
        let stack = PowerCircleStackView()
        stack.axis = .horizontal
        stack.spacing = 6
        stack.alignment = .center
        
        let dot = UIView()
        dot.backgroundColor = color
        dot.layer.cornerRadius = 5
        
        dot.snp.makeConstraints { make in
            make.width.height.equalTo(10)
        }
        
        let label = UILabel()
        label.text = title
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = color
        label.numberOfLines = 0
        label.setContentCompressionResistancePriority(.required, for: .vertical)
        
        stack.addArrangedSubview(dot)
        stack.addArrangedSubview(label)
        
        stack.snp.makeConstraints { make in
            make.height.greaterThanOrEqualTo(20)
        }
        
        stack.addTapGesturesWithTarget(target: self, action: #selector(stackTapped(_ :)))
        return stack
    }
    
    
    @objc func stackTapped(_ tap: UITapGestureRecognizer) {
        guard let tapView = tap.view as? PowerCircleStackView, let model = tapView.segment?.model else {
            return
        }
        
        onPowerCircleInfoTapped?(model)
    }
    

}
